<?php
$loader->requireOnce('includes/inPatientCore/InPatientCoreErrorCode.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientCoreException.class.php');
$loader->requireOnce('includes/CoreErrorCode.class.php');
$loader->requireOnce('includes/CoreException.class.php');
$loader->requireOnce('includes/EnumerationConstant.class.php');
$loader->requireOnce('includes/EmerQueueFactory.class.php');
$loader->requireOnce('includes/EmergencyInstantPanel.class.php');
$loader->requireOnce('includes/inPatientCore/TestDiagnosisTicketManagement.class.php');
$loader->requireOnce('includes/LabConnector/LabAssignmentConnector.class.php');
$loader->requireOnce('includes/LabConnector/LabResultConnector.class.php');

$loader->requireOnce('includes/EmerProcessor.class.php');


/**
 * Controler for processing test assign.
 * @package module.dept_emer.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_EmerTestDiagnosisTicket extends Controller {
	private $logger ;
	private $tpProcessor ;
	function __construct($template_mod = "general") {
		parent::Controller();
		$this->logger = LoggerManager::getLogger('module.dept_emer.controllers.C_EmerTestDiagnosisTicket');
		$this->template_mod = $template_mod;
		$this->view->path="patient_instant_info";
		$this->tpProcessor = new EmerProcessor($this);
	}

	public function actionLoadLabAssignPanelAjax() {
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''));
		$ticketId = $_GET['ticketId'];


		$this->logger->info('Start actionLoadLabAssignPanelAjax for patient has ticket ['. $ticketId .']');
		try {

			$labAssignmentConnector = new LabAssignmentConnector();
			$listOfLabAssignment = $labAssignmentConnector->listAssignments($ticketId);
			$result['result'] = $this->tpProcessor->loadLabAssignmentPanel($ticketId, $listOfLabAssignment);
				
				

		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionLoadLabAssignPanelAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionLoadLabAssignPanelAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionLoadLabAssignPanelAjax for patient has ticket ['. $ticketId .']');
		return json_encode($result);
	}


	public function actionBuildLabAssignmentAjax() {
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		$ticketId = $_GET['ticketId'];
		$idOfActionAfterCreating = $_GET['idOfActionAfterCreating'];


		$this->logger->info('Start actionBuildLabAssignmentAjax for patient has ticket ['. $ticketId .']');
		try {

			//$result['result'] = $this->tpProcessor->buildIPTestAssignDesktop($ticketId);
			$labAssignmentConnector = new LabAssignmentConnector();
			$result['result'] = $labAssignmentConnector->renderAssignmentPage($ticketId, $idOfActionAfterCreating);

		} catch (HISFileLoaderException $hisEx) {
			$result['error']['code'] = CoreErrorCode::LOAD_LAB_MODULE_FAIL;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_LAB_MODULE_FAIL);
			$this->logger->error('actionBuildLabAssignmentAjax for ticket [' . $ticketId . '] fail. Error detail: '. $hisEx->getMessage());

				
		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionBuildLabAssignmentAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionBuildLabAssignmentAjax for patient has ticket ['. $ticketId .']');
		return json_encode($result);


	}

	public function actionBuildLabAssignment4VEditAjax() {
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
		$ticketId = $_GET['ticketId'];
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$idOfActionAfterUpdating = $_GET['idOfActionAfterUpdating'];

		$this->logger->info('Start actionBuildLabAssignment4VEditAjax for patient has ticket ['. $ticketId .']');
		try {

			//$result['result'] = $this->tpProcessor->buildIPTestAssignDesktop($ticketId);
			$labAssignmentConnector = new LabAssignmentConnector();
			$result['result'] = $labAssignmentConnector->renderAssignmentPage4VEdit($ticketId, $labAssignmentTicketId, $idOfActionAfterUpdating);

		} catch (HISFileLoaderException $hisEx) {
			$result['error']['code'] = CoreErrorCode::LOAD_LAB_MODULE_FAIL;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_LAB_MODULE_FAIL);
			$this->logger->error('actionBuildLabAssignment4VEditAjax for ticket [' . $ticketId . '] fail. Error detail: '. $hisEx->getMessage());

			 
		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionBuildLabAssignment4VEditAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionBuildLabAssignment4VEditAjax for patient has ticket ['. $ticketId .']');
		return json_encode($result);


	}

	public function actionPrintAssignedTestTicket() {
		$ticketId = $_GET['ticketId'];
		$testDiagnosisTicketId = $_GET['testTicketId'];
		$this->logger->info('Start actionPrintAssignedTestTicket ');
	    try {

            //$result['result'] = $this->tpProcessor->buildIPTestAssignDesktop($ticketId);
            $labAssignmentConnector = new LabAssignmentConnector();
            echo $labAssignmentConnector->renderLabAssignment4Print($testDiagnosisTicketId);

        } catch (HISFileLoaderException $hisEx) {
            $result['error']['code'] = CoreErrorCode::LOAD_LAB_MODULE_FAIL;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_LAB_MODULE_FAIL);
            $this->logger->error('actionBuildLabAssignment4VEditAjax for ticket [' . $ticketId . '] fail. Error detail: '. $hisEx->getMessage());

             
        } catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionBuildLabAssignment4VEditAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish actionPrintAssignedTestTicket ');
	}

	public function actionEditAssignedTestAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''),'result'=>array());
		$ticketId = $_GET['ticketId'];
		$testDiagnosisTicketId = $_GET['testTicketId'];

		$this->logger->info('Start actionEditAssignedTestAjax for patient has ticket ['. $ticketId .']');
		try {

			$testTicketInfo = array(
            'requested_time'=>$_GET['requestedTime'],
            'doctor_id'=>$_GET['doctorId'],
            'diagnosis'=>$_GET['diagnosis']
			);

			$assignedTest = $_GET['testIds'];
			$testTicketManagerment = new  TestDiagnosisTicketManagement();
			$testTicketManagerment->editAssignedTest($testDiagnosisTicketId, $testTicketInfo, $assignedTest);

		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionEditAssignedTestAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}  catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionEditAssignedTestAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionEditAssignedTestAjax for patient has ticket ['. $ticketId .']');
		return json_encode($result);
			
	}


	public function actionGetTestOfGroup() {
		$result = array('error'=>array('code'=>0,'mess'=>''),'result'=>array());
		$testGroupId = $_GET['testGroupId'];

		$this->logger->info('Start actionGetTestOfGroup ');
		try {
			$testTicketManagerment = new  TestDiagnosisTicketManagement();
			$result['result']  =
			$testTicketManagerment->getListOfTestByGroup($testGroupId);
		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionGetTestOfGroup fail. Error detail: '. $ex->getMessage());
		}   catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionGetTestOfGroup fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionGetTestOfGroup ');
		return json_encode($result);
			
	}
	/**
	 * add new assigned tests set.
	 * @return unknown_type
	 */
	public function actionAddAssignedTestAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''),'result'=>array());
		$testGroupId = $_GET['testGroupId'];

		$this->logger->info('Start actionAddAssignedTestAjax ');
		try {
			$testTicketInfo = array('ticket_id'=>$_GET['ticketId'],
        	'requested_time'=>$_GET['requestedTime'],
        	'doctor_id'=>$_GET['doctorId'],
        	'diagnosis'=>$_GET['diagnosis'],
        	'is_done'=> 0
			);

			$assignedTest = $_GET['testIds'];
			$testTicketManagerment = new  TestDiagnosisTicketManagement();
			$result['result']['test_diagnosis_ticket_id'] =
			$testTicketManagerment->addAssignedTest($testTicketInfo, $assignedTest);


		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionAddAssignedTestAjax fail. Error detail: '. $ex->getMessage());
		}   catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionAddAssignedTestAjax fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionAddAssignedTestAjax ');
		return json_encode($result);
	}

	/**
	 * delete a test ticket.
	 * @return unknown_type
	 */
	public function actionDeleteLabAssignmentTicketAjax() {
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$this->logger->info('Start actionDeleteLabAssignmentTicketAjax [' . $labAssignmentTicketId . "]");
		try {


			$labAssignmentConnector = new LabAssignmentConnector();
			$deleteResult = $labAssignmentConnector->deleteAssignment($labAssignmentTicketId);

			if ($deleteResult['error']['code'] != CoreErrorCode::NORMAL) {
				$result['error']['code'] = $deleteResult['error']['code'];
				$result['error']['mess'] = $deleteResult['error']['mess'];
			} else {
				$result['result']= $deleteResult['result'];
			}

		} catch (HISFileLoaderException $hisEx) {
			$result['error']['code'] = CoreErrorCode::LOAD_LAB_MODULE_FAIL;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_LAB_MODULE_FAIL);
			$this->logger->error('actionDeleteLabAssignmentTicketAjax fail. Error detail: '. $hisEx->getMessage());
			 
		}
		$this->logger->info('Finish actionDeleteLabAssignmentTicketAjax [' . $labAssignmentTicketId . "]");
		return json_encode($result);
	}


	public function actionLoadLabResultAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
		$ticketId = $_GET['ticketId'];

		$this->logger->info('Start actionLoadLabResultAjax');
		try {

			$labResultConnector = new LabResultConnector();
			$result['result'] = $labResultConnector->viewLabResult($ticketId);

		} catch (HISFileLoaderException $hisEx) {
			$result['error']['code'] = CoreErrorCode::LOAD_LAB_MODULE_FAIL;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_LAB_MODULE_FAIL);
			$this->logger->error('actionLoadLabResultAjax for ticket [' . $ticketId . '] fail. Error detail: '. $hisEx->getMessage());

			 
		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionLoadLabResultAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionLoadLabResultAjax');
		return json_encode($result);
	}

	public function actionLoadTestItem4InputResultAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
		$ticketId = $_GET['ticketId'];
		$testDiagnosisTicketId = $_GET['testTicketId'];

		$this->logger->info('Start actionLoadTestItem4InputResult');
		try {
			$result['result'] = $this->tpProcessor->buildAssignedTestItemResultInput($ticketId, $testDiagnosisTicketId);

		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('Load actionLoadTestItem4InputResult fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionLoadTestItem4InputResult');
		return json_encode($result);

	}

	public function actionSaveTestItemResultAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
		$ticketId = $_GET['ticketId'];
		$testDiagnosisTicketId = $_GET['testTicketId'];
		$assignedTestDone = $_GET['assignedTestDone'];
		$assignedTestNotDone = $_GET['assignedTestNotDone'];

		$this->logger->info('Start actionSaveTestItemResultAjax');
		try {
				
			$testTicketManagerment = new  TestDiagnosisTicketManagement();
			$testTicketManagerment->saveTestItemResult($ticketId, $testDiagnosisTicketId, $assignedTestDone, $assignedTestNotDone);
			$testTicketManagerment->updateDoneTestTicket($testDiagnosisTicketId, 1);

		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('Load actionSaveTestItemResultAjax fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionSaveTestItemResultAjax');
		return json_encode($result);

	}

	public function actionUpdateDoneTestTicketAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''), 'result'=>array());
		$testDiagnosisTicketId = $_GET['testTicketId'];
		$isDone = $_GET['isDone'];
		$ticketId = $_GET['ticketId'];

		$this->logger->info('Start actionUpdateDoneTestTicketAjax');
		try {

			$testTicketManagerment = new  TestDiagnosisTicketManagement();
			$testTicketManagerment->updateDoneTestTicket($testDiagnosisTicketId, $isDone);
			$listOfAssignedTest = $testTicketManagerment->listOfTestTicket($ticketId);
			foreach ($listOfAssignedTest as &$testTicket) {
				TestDiagnosisTicketManagement::normalizeData4Display($testTicket);
				 
			}
			$result['result']['testTicketList']= $listOfAssignedTest;

		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('Load actionUpdateDoneTestTicketAjax fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish actionUpdateDoneTestTicketAjax');
		return json_encode($result);

	}





}
?>